package com.practicesuite.controllayer;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import com.practicesuite.systemlayer.Admin;
import com.practicesuite.systemlayer.MedicalInformation;
import com.practicesuite.systemlayer.Patient;
import com.practicesuite.systemlayer.SystemDriver;
import com.practicesuite.systemlayer.User;

/**
 * Panel for filling out information to register a new user in the system.
 * 
 * @author James
 *
 */
public class CreateUserPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private JTextField textFieldFirstName;
    private JTextField textFieldLastName;
    private JTextField textFieldAddress;
    private JTextField textFieldPhone;
    private JTextField textFieldEmail;
    private JTextField textFieldAge;
    private JTextField textFieldPharmacy;
    private JTextField textFieldInsurance;
    private JTextField textFieldAllergies;
    public MainWindow window;
    private final Admin admin;
    private JTextField textFieldUsername;
    private JPasswordField passwordField_1;
    private JPasswordField passwordField_2;

    /**
     * Create the panel.
     */
    public CreateUserPanel(MainWindow container, final User user) {
        this.window = container;
        admin = (Admin)user;
        setLayout(new GridLayout(0, 1, 0, 0));
        setPreferredSize(new Dimension(500, 400));
        
        JPanel panel_8 = new JPanel();
        panel_8.setLayout(new GridLayout(0, 1, 0, 0));
        panel_8.setPreferredSize(new Dimension(500, 500));
        
        JLabel labelHeader = new JLabel("Create New User Account");
        panel_8.add(labelHeader);
        labelHeader.setFont(new Font("Tahoma", Font.PLAIN, 24));
        labelHeader.setHorizontalAlignment(SwingConstants.CENTER);
        
        JPanel panel_6 = new JPanel();
        panel_8.add(panel_6);
        panel_6.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel lblUsername = new JLabel("Username: ");
        lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
        lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_6.add(lblUsername);
        
        textFieldUsername = new JTextField();
        panel_6.add(textFieldUsername);
        textFieldUsername.setColumns(10);
        
        JLabel lblPassword = new JLabel("Password: ");
        lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
        lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_6.add(lblPassword);
        
        passwordField_1 = new JPasswordField();
        panel_6.add(passwordField_1);
        
        JPanel panel_7 = new JPanel();
        panel_8.add(panel_7);
        panel_7.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel lblReenterPassword = new JLabel("Re-enter Password: ");
        lblReenterPassword.setHorizontalAlignment(SwingConstants.RIGHT);
        lblReenterPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_7.add(lblReenterPassword);
        
        passwordField_2 = new JPasswordField();
        panel_7.add(passwordField_2);
        
        JPanel panel_9 = new JPanel();
        panel_7.add(panel_9);
        
        JPanel panel = new JPanel();
        panel_8.add(panel);
        panel.setLayout(new GridLayout(0, 2));
        
        JLabel labelFirstName = new JLabel("First Name: ");
        labelFirstName.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelFirstName.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(labelFirstName);
        
        textFieldFirstName = new JTextField();
        panel.add(textFieldFirstName);
        textFieldFirstName.setColumns(10);
        
        JLabel labelLastName = new JLabel("Last Name: ");
        labelLastName.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelLastName.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(labelLastName);
        
        textFieldLastName = new JTextField();
        panel.add(textFieldLastName);
        textFieldLastName.setColumns(10);
        
        JPanel panel_1 = new JPanel();
        panel_8.add(panel_1);
        panel_1.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelAddress = new JLabel("Street Address: ");
        panel_1.add(labelAddress);
        labelAddress.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelAddress.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldAddress = new JTextField();
        panel_1.add(textFieldAddress);
        textFieldAddress.setColumns(10);
        
        JLabel labelPhone = new JLabel("Phone Number [(###) ###-####]: ");
        panel_1.add(labelPhone);
        labelPhone.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelPhone.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldPhone = new JTextField();
        panel_1.add(textFieldPhone);
        textFieldPhone.setColumns(10);
        
        JPanel panel_2 = new JPanel();
        panel_8.add(panel_2);
        panel_2.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelEmail = new JLabel("Email Address: ");
        panel_2.add(labelEmail);
        labelEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelEmail.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldEmail = new JTextField();
        panel_2.add(textFieldEmail);
        textFieldEmail.setColumns(10);
        
        JLabel labelGender = new JLabel("Gender: ");
        panel_2.add(labelGender);
        labelGender.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelGender.setHorizontalAlignment(SwingConstants.RIGHT);
        
        final JComboBox comboBoxGender = new JComboBox();
        comboBoxGender.setFont(new Font("Tahoma", Font.PLAIN, 12));
        comboBoxGender.setModel(new DefaultComboBoxModel(new String[] {"Male", "Female"}));
        comboBoxGender.setSelectedIndex(0);
        panel_2.add(comboBoxGender);
        
        JPanel panel_4 = new JPanel();
        panel_8.add(panel_4);
        panel_4.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelAge = new JLabel("Age: ");
        panel_4.add(labelAge);
        labelAge.setFont(new Font("Tahoma", Font.PLAIN, 12));
        labelAge.setHorizontalAlignment(SwingConstants.RIGHT);
        
        textFieldAge = new JTextField();
        panel_4.add(textFieldAge);
        textFieldAge.setColumns(10);
        
        JLabel labelPharmacy = new JLabel("Pharmacy: ");
        panel_4.add(labelPharmacy);
        labelPharmacy.setHorizontalAlignment(SwingConstants.RIGHT);
        labelPharmacy.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldPharmacy = new JTextField();
        panel_4.add(textFieldPharmacy);
        textFieldPharmacy.setColumns(10);
        
        JPanel panel_3 = new JPanel();
        panel_8.add(panel_3);
        panel_3.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel labelInsurance = new JLabel("Insurance Carrier: ");
        panel_3.add(labelInsurance);
        labelInsurance.setHorizontalAlignment(SwingConstants.RIGHT);
        labelInsurance.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldInsurance = new JTextField();
        panel_3.add(textFieldInsurance);
        textFieldInsurance.setColumns(10);
        
        JLabel labelAllergies = new JLabel("Allergies: ");
        panel_3.add(labelAllergies);
        labelAllergies.setHorizontalAlignment(SwingConstants.RIGHT);
        labelAllergies.setFont(new Font("Tahoma", Font.PLAIN, 12));
        
        textFieldAllergies = new JTextField();
        panel_3.add(textFieldAllergies);
        textFieldAllergies.setColumns(10);
        
        JPanel panel_11 = new JPanel();
        panel_8.add(panel_11);
        panel_11.setLayout(new GridLayout(0, 2, 0, 0));
        
        JLabel lblUserType = new JLabel("User Type: ");
        lblUserType.setHorizontalAlignment(SwingConstants.RIGHT);
        lblUserType.setFont(new Font("Tahoma", Font.PLAIN, 12));
        panel_11.add(lblUserType);
        
        final JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Select", "Patient", "Nurse", "Doctor", "Administrator"}));
        panel_11.add(comboBox);
        
        JPanel panel_12 = new JPanel();
        panel_11.add(panel_12);
        
        JPanel panel_5 = new JPanel();
        panel_8.add(panel_5);
        
        JButton btnSignup = new JButton("Create Account");
        btnSignup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    char[] password1 = passwordField_1.getPassword();
                    char[] password2 = passwordField_2.getPassword();
                    if ((String.valueOf(password1)).equals(String.valueOf(password2))) {
                        String username = textFieldUsername.getText();
                        String name = textFieldLastName.getText() + ", " + textFieldFirstName.getText();
                        String address = textFieldAddress.getText();
                        String phoneNumber = textFieldPhone.getText();
                        String email = textFieldEmail.getText();
                        String gender = (String)comboBoxGender.getSelectedItem();;
                        int age = Integer.parseInt(textFieldAge.getText());
                        String pharmacy = textFieldPharmacy.getText();
                        String insurance = textFieldInsurance.getText();
                        String allergies = textFieldAllergies.getText();
                        
                        
                        GregorianCalendar calendar = new GregorianCalendar();
                        Date dateOfBirth = calendar.getTime();
                        
                        try {
                            if (comboBox.getSelectedIndex() == 0) {
                                JOptionPane.showMessageDialog(null, "Select a valid user type.");
                            }
                            else if (comboBox.getSelectedIndex() == 1) {
                                MedicalInformation medicalInfo = new MedicalInformation(0, pharmacy, allergies, insurance);
                                SystemDriver.createPatient(name, dateOfBirth, address, phoneNumber, 
                                        gender, age, email, username, password1, 3, medicalInfo);
                                window.gotoPanel("adminHome", admin);
                            }
                            else if (comboBox.getSelectedIndex() == 2) {
                                MedicalInformation medicalInfo = new MedicalInformation(0, pharmacy, allergies, insurance);
                                Patient p = SystemDriver.createPatient(name, dateOfBirth, address, phoneNumber, 
                                        gender, age, email, username, password1, 3, medicalInfo);
                                SystemDriver.editUser(p, p.getUserID(), 2, false);
                                window.gotoPanel("adminHome", admin);
                            }
                            else if (comboBox.getSelectedIndex() == 3) {
                                MedicalInformation medicalInfo = new MedicalInformation(0, pharmacy, allergies, insurance);
                                Patient p = SystemDriver.createPatient(name, dateOfBirth, address, phoneNumber, 
                                        gender, age, email, username, password1, 3, medicalInfo);
                                SystemDriver.editUser(p, p.getUserID(), 1, false);
                                window.gotoPanel("adminHome", admin);
                            }
                            else if (comboBox.getSelectedIndex() == 4) {
                                MedicalInformation medicalInfo = new MedicalInformation(0, pharmacy, allergies, insurance);
                                Patient p = SystemDriver.createPatient(name, dateOfBirth, address, phoneNumber, 
                                        gender, age, email, username, password1, 3, medicalInfo);
                                SystemDriver.editUser(p, p.getUserID(), 0, false);
                                window.gotoPanel("adminHome", admin);
                            }
                        } catch (Exception e) {
                            JOptionPane.showMessageDialog(null, "System error, contact administrator.");
                        }
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Passwords do not match, please retype password.");
                    }
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Invalid input.");
                }
            }
        });
        panel_5.add(btnSignup);
        btnSignup.setFont(new Font("Tahoma", Font.PLAIN, 14));
        
        JButton btnReturn = new JButton("Cancel");
        btnReturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                window.gotoPanel("adminHome", admin);
            }
        });
        btnReturn.setFont(new Font("Tahoma", Font.PLAIN, 14));
        panel_5.add(btnReturn);
        
        JScrollPane scrollPane = new JScrollPane(panel_8);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane);
    }
}
